<template>
  <view>
    <customtop top_title="公司列表"></customtop>
    <view class="company_list" v-if="!data_list_loading">
      <view class="container">
        <view v-if="data_list.length > 0">
          <view class="items">
            <view class="item" v-for="(item, index) in data_list" :key="index" @click="jumpPage('/pages/company/show?id=' + item.id)">
              <image class="cover" :src="item.logo" mode="aspectFill" />
              <view class="maininfo">
                <view class="name">{{item.shortname}}</view>
                <view class="dec">{{item.name}}</view>
                <view class="dec">{{item.industry}}行业</view>
                <view class="dec">
                  <span class="dec_item">{{item.user_count}}个员工</span>
                  <span class="dec_item">{{item.post_count}}条发布信息</span>
                </view>
              </view>
            </view>
          </view>
          <view class="uloadmore" @click="getMore"><u-loadmore :status="loadmore_status" /></view>
        </view>
        <u-empty
          v-if="data_list.length == 0"
          mode="data"
          icon="http://cdn.uviewui.com/uview/empty/data.png"
          text="暂无内容~"
        >
        </u-empty>
      </view>
    </view>
  </view>
</template>

<script>
import { request } from "@/utils/http.js"
import customtop from "@/components/customtop.vue"
export default {
  components: { customtop },

  data() {
    return {
      data_list: [],
      data_list_loading: true,
      loadmore_status: 'loadmore',
      loadmore_finished: false,
      params: {
        page_size: 15,
        page: 1,
      },
    }
  },

  onLoad(options) {
    uni.showLoading();
    this.getCompanysPaginate();
  },

  onReachBottom() {
    this.getMore();
  },

  methods: {
    getCompanysPaginate: function() {
      let params = this.params;
      params.company_id = this.id;
      request.post('/company/getCompanysPaginate', params).then(res => {
        uni.stopPullDownRefresh();
        uni.hideLoading();
        this.data_list_loading = false;

        // 返回数据为空
        if (res.data.total == 0) {
          this.data_list = [];
          return false;
        }

        // 组装数据
        if (res.data.current_page == 1) {
          this.data_list = res.data.data;
        } else {
          this.data_list = this.data_list.concat(res.data.data);
        }

        // 最后一页
        if (this.params.page == res.data.last_page) {
          this.loadmore_finished = true;
          this.loadmore_status = 'nomore';
          return false;
        }

        this.params.page = parseInt(res.data.current_page) + parseInt(1);
        this.loadmore_status = 'loadmore';
        this.loadmore_finished = false;
      })
    },

    getMore: function() {
      if (!this.loadmore_finished) {
        this.loadmore_status = 'loading';
        this.getCompanysPaginate();
      }
    },

    switchTab: function(url) {
      uni.switchTab({ url: url });
    },

    jumpPage: function(url) {
      uni.navigateTo({ url: url });
    }
  }
}
</script>

<style>
@import url("company.css");
page {
  padding-bottom: 40rpx;
}
.uloadmore {
  margin: 30rpx 0;
}
</style>
